package com.diyunapp.xilinapp.me;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginPassNextActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.new_pass_edt)
    EditText newPassEdt;
    @Bind(R.id.sure_pass_edt)
    EditText surePassEdt;
    @Bind(R.id.sure_to_change)
    TextView sureToChange;
    private String member_id;
    private View headView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_pass_next);
        ButterKnife.bind(this);
        headView = findViewById(R.id.lgpn_menubar_inc);
        super.MenuWidget(headView);
        SharedPreferences userinfo=getSharedPreferences("user_info",MODE_PRIVATE);
        member_id=userinfo.getString("member_id","");

    }
    @OnClick({R.id.go_back, R.id.sure_to_change})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                Intent intent = new Intent();
                intent.setClass(this, RechargePassActivity.class);
                startActivity(intent);
                break;
            case R.id.sure_to_change:
                if(newPassEdt.getText().toString().trim()==""){
                    Toast.makeText(this,"请输入新密码",Toast.LENGTH_SHORT).show();
                }
                else if(surePassEdt.getText().toString().trim()==""){
                    Toast.makeText(this,"请确认新密码",Toast.LENGTH_SHORT).show();
                }
                else{
                    Change(member_id);
                }
                break;
        }
    }

    private void Change(String id) {
        String pass=newPassEdt.getText().toString().trim();
        String surePass=surePassEdt.getText().toString().trim();
        String url=""+MainActivity.url+"act=goods&op=login_pwd&member_id="+ MainActivity.myLogin.getMemberid()+"&password="+pass+"&repassword="+surePass+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int code = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            String data= resultObject.optString("data");
                            if (code==200) {

                                toast(data);
                                setResult(1);
                                finish();
                            } else {

                                toast(data);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");

                    }
                });
    }
}
